﻿using System;
using System.Linq;
using EnumFields.Model;
using NUnit.Framework;
using Xtensive.Orm;
using Xtensive.Orm.Configuration;

namespace EnumFields
{
    public class Program
    {
        [Test]
        public static void Main()
        {
            // Loading domain configuration section with name "Default".
            var domainConfiguration = DomainConfiguration.Load("Default");
            var domain = Domain.Build(domainConfiguration);

            using (var session = domain.OpenSession()) {
                using (var t = session.OpenTransaction()) {
                    var lotr = new Book(session) {
                        Title = "The Lord Of The Rings",
                        Genres = BookGenres.Adventure | BookGenres.Fantasy
                    };

                    var dou = new Book(session) {
                        Title = "DataObjects.Net Unleashed",
                        Genres = BookGenres.Computers
                    };
                    t.Complete();
                }

                using (var tx = session.OpenTransaction()) {
                    // Fetching adventure books
                    var adventures = session.Query.All<Book>().Where(b => b.Genres.HasFlag(BookGenres.Adventure)).ToList();

                    foreach (var book in adventures) {
                        Console.WriteLine("{0}. {1}", book.Title, book.Genres);
                    }

                    tx.Complete();
                }
            }
        }
    }
}
